package com.example.controller;

import com.example.config.IdGeneratorFactory;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author yudong
 * @Date 2022/9/19 17:52
 * @description：
 */
@Slf4j
@RestController
public class RedisController {

    @Autowired
    private IdGeneratorFactory idGeneratorFactory;

// --------------------- 分布式自增Id 测试 ---------------------------

    @ApiOperation("根据key生成自增Id")
    @GetMapping(value = "/getId")
    public Long getId(@RequestParam("key") String key) {
        log.info("生成Id中");
        long generate = idGeneratorFactory.generate(key, IdGeneratorFactory.getTodayEndTime());
        log.info("generate={}", generate);
        return generate;
    }


    @ApiOperation("根据key生成自增Id,日期版")
    @GetMapping(value = "/getIdByToday")
    public String getIdByToday(@RequestParam("key") String key) {
        log.info("生成Id中");
        String id = idGeneratorFactory.generateIdByToday(key, 3);
        log.info("id={}", id);
        return id;
    }
}
